package com.bosssoft.utils;

import com.bosssoft.entity.po.FibVoucherDetailsPO;
import com.bosssoft.entity.vo.FibVoucherDetailsVO;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

/**
 * @author 项昊天
 * @create 2021/8/27 17:01
 */
@Slf4j
public class MyClassCastUtil {
  public static FibVoucherDetailsPO castObjectToVDPO(Object o) {
    log.info("进入到强制转换方法");
    String kV = o.toString();
    FibVoucherDetailsPO fibVoucherDetailsPO = new FibVoucherDetailsPO();
    String[] keysValues = kV.split(",");
    log.info("String[]:{}", keysValues);
    String[][] keyAndValue = new String[keysValues.length][2];
    log.info("string[][]:{}", keyAndValue);

    int i = 0;
    for (String keysValue : keysValues) {
      keyAndValue[i] = keysValue.split("=");
      log.info("keyAndValue[i]:{}", keyAndValue[i]);
    }
    HashMap<String, Object> map = new HashMap<>(keysValues.length);
    for (int j = 0; j < keyAndValue.length; j++) {
      map.put(keyAndValue[j][0], keyAndValue[j][1]);
    }

    log.info("map:{}", map);
    fibVoucherDetailsPO.setVoucherId((Long)map.get("voucherId"));
    fibVoucherDetailsPO.setBgtId((Long)map.get("bgtId"));
    fibVoucherDetailsPO.setVoucherCode((Integer)map.get("voucherCode"));
    fibVoucherDetailsPO.setVoucherAmount((BigDecimal) map.get("voucherAmount"));
    fibVoucherDetailsPO.setVoucherAccountUserid((Long) map.get("vucherAccountUserid"));
    fibVoucherDetailsPO.setVoucherPayUserid((Long) map.get("voucherPayUserid"));
    fibVoucherDetailsPO.setStatus((Integer) map.get("status"));
    fibVoucherDetailsPO.setVoucherUrlNotSeal(map.get("voucherUrlNotSeal").toString());
    fibVoucherDetailsPO.setVoucherUrl(null);
    fibVoucherDetailsPO.setCreateBy(map.get("createBy").toString());
    fibVoucherDetailsPO.setCreateTime((Date) map.get("createTime"));
    fibVoucherDetailsPO.setDeleted((Byte) map.get("deleted"));
    fibVoucherDetailsPO.setVersion((Integer) map.get("version"));

    return fibVoucherDetailsPO;
  }

  public static FibVoucherDetailsVO castObjectToVDVO(Object o) {
    String kv = o.toString();
    String[] split = kv.split(",");
    String[][] keysValues = new String[split.length][2];
    int i = 0;
    for (String s : split) {
      keysValues[i] = s.split("=");
      i ++;
    }
    HashMap<String, Object> map = new HashMap<>();
    for (int k = 0; k < keysValues.length; k++) {
      map.put(keysValues[k][0], keysValues[k][1]);
    }
    FibVoucherDetailsVO fibVoucherDetailsVO = new FibVoucherDetailsVO();
    fibVoucherDetailsVO.setVoucher_location_id((Integer) map.get("voucher_location_id"));
    fibVoucherDetailsVO.setSealId((Long) map.get("sealId"));
    fibVoucherDetailsVO.setVoucherId((Long) map.get("voucherId"));

    return fibVoucherDetailsVO;
  }
}
